//
//  CellForColumCollection.m
//  MY Project For Sport
//
//  Created by dllo on 16/3/3.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "CellForColumCollection.h"
#import "NetworkingHandlerOfBlock.h"
#import "CollectionCellForColumn.h"
#import "ModelForColumn.h"
#import <DKNightVersion.h>
#import "UIColor+Wonderful.h"
#define WIDTH self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height

@interface CellForColumCollection ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionView *collection;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) UICollectionViewFlowLayout *layout;


@end


@implementation CellForColumCollection

-(void)dealloc {
    
    [_collection release];
    [_array release];
    [_layout release];
    [super dealloc];
    
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {

        [self createCollection];
        [self handleData];
    }

    return self;

}

- (void)createCollection {

 
    
    self.layout = [[UICollectionViewFlowLayout alloc] init];
    
    self.collection = [[UICollectionView alloc] initWithFrame:self.contentView.frame collectionViewLayout:self.layout];
    [self.contentView addSubview:self.collection];
    [_layout release];
    [_collection release];
    //self.collection.dk_backgroundColorPicker = DKColorWithColors([UIColor linenColor], [UIColor grayColor]);

    self.collection.showsHorizontalScrollIndicator = NO;
    self.collection.backgroundColor = [UIColor whiteColor];
    self.collection.delegate = self;
    self.collection.dataSource = self;
    
    [self.collection registerClass:[CollectionCellForColumn class] forCellWithReuseIdentifier:@"poolForHeader"];
    
}

- (void)handleData {

    NSString *str = @"http://platform.sina.com.cn/sports_client/z_list";
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:str]];
    request.HTTPMethod = @"POST";
    request.HTTPBody = [@"app_key=2586208540&_version__=3.2.1.01&__os__=android&list_type=recmd" dataUsingEncoding:NSUTF8StringEncoding];
    NSURLSession *session = [NSURLSession sharedSession];
    
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            id result = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error:nil];
            //NSLog(@"%@", result);
            
            self.array = [NSMutableArray array];
            
            NSDictionary *dic = [result objectForKey:@"result"];
            
            NSDictionary *dict = [dic objectForKey:@"data"];
            
            
            NSArray *array = [dict objectForKey:@"list"];
            
            
            for (NSDictionary *dicd in array) {
                
                ModelForColumn *model = [[ModelForColumn alloc] init];
                
                [model setValuesForKeysWithDictionary:dicd];
                
                
                [self.array addObject:model];
                
                [model release];
                
            }
            
            [self.array removeObjectAtIndex:0];
            [self.collection reloadData];
            
        });
        
    }];
    
    [task resume];

}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.array.count;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    CollectionCellForColumn *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"poolForHeader" forIndexPath:indexPath];
    
    [cell passModelForColumn:[self.array objectAtIndex:indexPath.item]];
    

    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    ModelForColumn *model = [self.array objectAtIndex:indexPath.row];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"next" object:nil userInfo:@{@"ID":model._id, @"read":@(model.fans_num)}];
    

}



- (void)layoutSubviews {
    
    [super layoutSubviews];

   self.layout.itemSize = CGSizeMake((WIDTH - 10 - 15 * 3) / 4 , HEIGHT - 8);
   self.layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    self.layout.sectionInset = UIEdgeInsetsMake(2, 10, 2, 10);
    self.collection.frame = CGRectMake(0, 2, WIDTH, HEIGHT - 4);
    
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
